package com.bookmanger.server.controller;

import com.bookmanger.common.result.Result;
import com.bookmanger.server.service.RankingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ranking")
public class RankingController {

    @Autowired
    private RankingService rankingService;

    /**
     * 管理员同意出借某本书并更新排行榜
     * @param borrowId 借阅记录ID
     * @return 操作结果
     */
    @PostMapping("/updateSet")
    public Result updateSet(@RequestParam int borrowId) {
        try {
            rankingService.updateSet(borrowId);
            return Result.success("操作成功");
        } catch (Exception e) {
            return Result.error("操作失败: " + e.getMessage());
        }
    }

    /**
     * 获取借阅排行榜
     * @return 排行榜数据
     */
    @GetMapping("/getRanking")
    public Result getBorrowRanking() {
        try {
            return Result.success(rankingService.getBorrowRanking());
        } catch (Exception e) {
            return Result.error("获取排行榜失败: " + e.getMessage());
        }
    }
}